package com.rjkj.medicalwaste.ui.setting

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.adapter.ItemClick
import com.rjkj.medicalwaste.adapter.SearchHospitalAdapter
import com.rjkj.medicalwaste.base.BaseFragment
import com.rjkj.medicalwaste.bean.HospitalBean
import com.rjkj.medicalwaste.bean.MedicalWasteType
import com.rjkj.medicalwaste.databinding.FragmentPrintItemBinding
import com.rjkj.medicalwaste.databinding.FragmentSearchHospitalBinding
import com.rjkj.medicalwaste.databinding.ItemHospitalListBinding
import com.rjkj.medicalwaste.databinding.ItemPrintChooseBinding
import com.rjkj.medicalwaste.viewmodels.UserViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider


class PrintItemFragment : BaseFragment<FragmentPrintItemBinding>() {
    private lateinit var printItemAdapter: PrintItemAdapter
    private val userViewModel: UserViewModel by activityViewModels {
        MainViewModelProvider.provideUserModel(requireContext())
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = FragmentPrintItemBinding.inflate(inflater, container, false)
        onSubscribeUi(binding)
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

    override fun onSubscribeUi(binding: FragmentPrintItemBinding) {
        initTip(userViewModel)
        printItemAdapter = PrintItemAdapter(userViewModel.setting.value?.printItemList ?: listOf(),
            object : ItemClick<PrintItem> {
                override fun onClicked(view: View, value: PrintItem) {
                    if (value.canClick) {
                        val list = userViewModel.setting.value?.printItemList ?: listOf()
                        if (value.choose||list.filter { it.choose }.size < 7) {
                            value.choose = !value.choose
                            printItemAdapter.notifyDataSetChanged()
                            userViewModel.setting.value?.change = true
                            userViewModel.setting.value = (userViewModel.setting.value)
                        } else {
                            showTip("数量过多")
                        }

                    } else {
                        showTip("无法取消")
                    }

                }
            })
        binding.imBack.setOnClickListener {
            findNavController().navigateUp()
        }
        binding.itemList.apply {
            layoutManager = GridLayoutManager(requireContext(), 3)
//            layoutManager = LinearLayoutManager(requireContext())
            adapter = printItemAdapter
        }
    }
}

data class PrintItem(var choose: Boolean = false, var type: String, var canClick: Boolean = true){
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as PrintItem

        if (type != other.type) return false

        return true
    }

    override fun hashCode(): Int {
        return type.hashCode()
    }
}

class PrintItemAdapter(val list: List<PrintItem>, val itemClick: ItemClick<PrintItem>) :
    RecyclerView.Adapter<PrintItemAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PrintItemAdapter.ViewHolder {
        val binding = ItemPrintChooseBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PrintItemAdapter.ViewHolder, position: Int) {
        holder.bind(list[position], itemClick)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(private val binding: ItemPrintChooseBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: PrintItem, itemClick: ItemClick<PrintItem>) {
            binding.printItem = item
            binding.root.setOnClickListener {
                itemClick.onClicked(it, item)
            }
            binding.executePendingBindings()
        }
    }
}